﻿using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;

using System.IO;
using System.Data;
using System.Text.RegularExpressions;
using System.Xml;
using System.Web.Security;
using System.Security.Cryptography;
using System.Net;
using System.Web;
using System.Runtime.InteropServices;

namespace Gift.WebApp
{
    /// <summary>
    ///ClinetMAC 的摘要说明
    /// </summary>
    public class ClinetMAC
    {
        // Methods
        public static string GetClientIP()
        {
            string userHostAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            switch (userHostAddress)
            {
                case null:
                case "":
                    userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    break;
            }
            if ((userHostAddress == null) || (userHostAddress == string.Empty))
            {
                userHostAddress = HttpContext.Current.Request.UserHostAddress;
            }
            return userHostAddress;
        }

        public string GetClinetMAC()
        {
            try
            {
                string userHostAddress = HttpContext.Current.Request.UserHostAddress;
                int dest = inet_addr(HttpContext.Current.Request.UserHostAddress.ToString().Trim());
                int num2 = inet_addr("");
                long mac = 0L;
                int length = 6;
                int num5 = SendARP(dest, 0, ref mac, ref length);
                string str3 = mac.ToString("X");
                while (str3.Length < 12)
                {
                    str3 = str3.Insert(0, "0");
                }
                string str4 = "";
                for (int i = 0; i < 11; i++)
                {
                    if (0 == (i % 2))
                    {
                        if (i == 10)
                        {
                            str4 = str4.Insert(0, str3.Substring(i, 2));
                        }
                        else
                        {
                            str4 = "-" + str4.Insert(0, str3.Substring(i, 2));
                        }
                    }
                }
                return str4;
            }
            catch
            {
                return null;
            }
        }

        public string GetIPAddress()
        {
            string userHostAddress = string.Empty;
            userHostAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if ((userHostAddress != null) && (userHostAddress != string.Empty))
            {
                if (userHostAddress.IndexOf(".") == -1)
                {
                    userHostAddress = null;
                }
                else if (userHostAddress.IndexOf(",") != -1)
                {
                    userHostAddress = userHostAddress.Replace(" ", "").Replace("'", "");
                    string[] strArray = userHostAddress.Split(",;".ToCharArray());
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        if (((this.IsIPAddress(strArray[i]) && (strArray[i].Substring(0, 3) != "10.")) && (strArray[i].Substring(0, 7) != "192.168")) && (strArray[i].Substring(0, 7) != "172.16."))
                        {
                            return strArray[i];
                        }
                    }
                }
                else
                {
                    if (this.IsIPAddress(userHostAddress))
                    {
                        return userHostAddress;
                    }
                    userHostAddress = null;
                }
            }
            string str2 = ((HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) && (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != string.Empty)) ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            switch (userHostAddress)
            {
                case null:
                case "":
                    userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    break;
            }
            if ((userHostAddress == null) || (userHostAddress == string.Empty))
            {
                userHostAddress = HttpContext.Current.Request.UserHostAddress;
            }
            return userHostAddress;
        }

        [DllImport("Ws2_32.dll")]
        private static extern int inet_addr(string ip);
        public bool IsIPAddress(string str1)
        {
            if ((((str1 == null) || (str1 == string.Empty)) || (str1.Length < 7)) || (str1.Length > 15))
            {
                return false;
            }
            string pattern = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$";
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
            return regex.IsMatch(str1);
        }

        [DllImport("Iphlpapi.dll")]
        private static extern int SendARP(int dest, int host, ref long mac, ref int length);

    }
}